package org.com.coolfish.scheduler;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.com.coolfish.common.database.entity.KuyuAddPackage;
import org.com.coolfish.common.database.service.KuyuAddPackageService;
import org.com.coolfish.common.database.service.KuyuCardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class CardChangePackageScheduler {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	private KuyuAddPackageService kuyuAddPackageService;
	@Autowired
	private KuyuCardService kuyuCardService;
	
    //获取当天的开始时间
    public  Date getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    //获取当天的结束时间
    public Date  getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
    
   
    //获取昨天的开始时间
    public Date getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //获取昨天的结束时间
    public Date getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    
	
	/**
	 * 27日执行
	 */
    //@Scheduled(cron ="0 0/1 * * * ?")
    @Scheduled(cron ="0 0 0 27 * ?")
	public void changeCUCCPackageScheduler() {
		List<String> operatorNameList = Arrays.asList("苏州联通8","苏州联通2","苏州联通");
		
		changeAddPackage();
		/*
		List<KuyuAddPackage> addPackageName = kuyuAddPackageService.findOperatorName();
		for (KuyuAddPackage kuyuAddPackage : addPackageName) {
			if ( operatorNameList.contains(kuyuAddPackage.getOperatorName()) ) {
				//联通操作
				changeAddPackage();
			} 
		}
		*/
		
	}
	/**
	 * 1日执行
	 */
    //@Scheduled(cron ="0 22 23 26 * ?")
    @Scheduled(cron ="0 0 0 1 * ?")
	public void changePackageScheduler() {
		
		changeAddPackage();
	}
	
	
	private void changeAddPackage() {
		long startTime = System.currentTimeMillis();
		logger.info("------- 开始执行时间:{}, 时间戳:{}",
                sdf.format(new Date(startTime)), startTime);
		//查询需要过期的套餐一次性变更
		kuyuAddPackageService.queryUnusePackageAndChangeStatus(getBeginDayOfYesterday(), getEndDayOfYesterDay());

		//查询要开启的套餐,一次性变更
		//kuyuAddPackageService.queryToopenPackageAndChangeType(getDayBegin());
		logger.info("开始变更属性");
		//查询要开启的套餐
		List<KuyuAddPackage> queryToopenPackage = kuyuAddPackageService.queryToopenPackage(getDayBegin());
		for (KuyuAddPackage kuyuAddPackage : queryToopenPackage) {
			//一次性变更
			kuyuAddPackageService.changeType(kuyuAddPackage.getId());
			
			//修改card表里面的packageid
			kuyuCardService.changeCardPackageid(kuyuAddPackage.getCardId(),kuyuAddPackage.getPackageid());
		}
		long endTime = System.currentTimeMillis();
		logger.info("-------结束执行时间:{}, 时间戳:{}, 耗时:{}ms",
                sdf.format(new Date(endTime)), endTime, (endTime - startTime));
	}
	
	
}
